#!/bin/bash

# 必须使用root权限运行
if [ "$(id -u)" -ne 0 ]; then
    echo "请使用 sudo 或 root 用户运行此脚本"
    exit 1
fi

# 定义公共变量 - 镜像加速源
REGISTRY_MIRRORS='[
    "https://docker.1ms.run",
    "https://docker.1panel.live",
    "https://docker.1panel.dev",
    "https://docker.1panel.top",
    "https://docker.1panelproxy.com",
    "https://docker.amingg.com",
    "https://docker.anye.in",
    "https://docker.m.daocloud.io",
    "https://dockerproxy.1panel.live",
    "https://hub.rat.dev",
    "https://proxy.1panel.live",
    "https://docker.13140521.xyz",
    "https://hub-mirror.c.163.com",
    "https://docker.m.daocloud.io",
    "https://dockerhub.anzu.vip",
    "https://mirror.baidubce.com",
    "https://docker.nju.edu.cn",
    "https://do.nark.eu.org",
    "https://dockerhub.azk8s.cn",
    "https://docker.ketches.cn"
]'

# 检测系统类型
if [ -f "/etc/synoinfo.conf" ]; then
    # 提取群晖版本号
    DSM_VERSION=$(grep 'udc_check_state=' /etc/synoinfo.conf | cut -d'"' -f2)
    DSM_MAJOR_VERSION=$(echo "$DSM_VERSION" | cut -d'.' -f1)
    DSM_MINOR_VERSION=$(echo "$DSM_VERSION" | cut -d'.' -f2)
    
    echo "[+] 检测到群晖系统 DSM ${DSM_VERSION}"
    
    # 根据版本设置不同的配置
    if [ "$DSM_MAJOR_VERSION" = "6" ]; then
        echo "[+] 检测到 DSM 6.x 系统"
        DOCKER_CONF="/var/packages/Docker/etc/dockerd.json"
        RESTART_CMD="synoservice --restart pkgctl-Docker"
        
        # 检测文件系统类型
        FS_TYPE=$(df -T /var/packages/Docker/target/docker | awk 'NR==2 {print $2}')
        echo "[+] 检测到文件系统类型: ${FS_TYPE}"
        
        if [ "$FS_TYPE" = "ext4" ]; then
            STORAGE_DRIVER="aufs"  # ext4 用 aufs
        else
            STORAGE_DRIVER="btrfs" # 其它（如 btrfs）用 btrfs
        fi
        
        DOCKER_CONFIG='{
            "data-root" : "/var/packages/Docker/target/docker",
            "log-driver" : "json-file",
            "registry-mirrors": '"${REGISTRY_MIRRORS}"',
            "storage-driver" : "'"${STORAGE_DRIVER}"'"
        }'
    elif [ "$DSM_MAJOR_VERSION" = "7" ] && [ "$DSM_MINOR_VERSION" -le "1" ]; then
        echo "[+] 检测到 DSM 7.0-7.1 系统"
        DOCKER_CONF="/var/packages/Docker/etc/dockerd.json"
        RESTART_CMD="synopkg restart Docker"
        DOCKER_CONFIG='{
            "data-root" : "/var/packages/Docker/var/docker",
            "log-driver" : "db",
            "registry-mirrors": '"${REGISTRY_MIRRORS}"',
            "storage-driver" : "btrfs"
        }'
    else
        echo "[+] 检测到 DSM 7.2+ 系统"
        DOCKER_CONF="/var/packages/ContainerManager/etc/docker.json"
        RESTART_CMD="synopkg restart ContainerManager"
        DOCKER_CONFIG='{
            "data-root": "/var/packages/ContainerManager/var/docker",
            "log-driver": "json-file",
            "registry-mirrors": '"${REGISTRY_MIRRORS}"',
            "storage-driver": "btrfs"
        }'
    fi
else
    echo "[+] 检测到通用Linux系统"
    DOCKER_CONF="/etc/docker/daemon.json"
    RESTART_CMD="systemctl restart docker"
    DOCKER_CONFIG='{
        "registry-mirrors": '"${REGISTRY_MIRRORS}"'
    }'
fi

# 创建配置目录（如果不存在）
mkdir -p "$(dirname "${DOCKER_CONF}")"

# 备份原有配置
if [ -f "${DOCKER_CONF}" ]; then
    cp "${DOCKER_CONF}" "${DOCKER_CONF}.bak"
    echo "[+] 原配置文件已备份至 ${DOCKER_CONF}.bak"
fi

# 写入新配置
echo "${DOCKER_CONFIG}" > "${DOCKER_CONF}"

# 设置文件权限
chmod 600 "${DOCKER_CONF}"
echo "[+] 新配置已写入 ${DOCKER_CONF}"

# 重启Docker服务
if eval "${RESTART_CMD}"; then
    echo "[+] Docker服务重启成功"
else
    echo "[-] Docker服务重启失败，请检查以下可能原因："
    if [ -f "/etc/synoinfo.conf" ]; then
        if [ "$DSM_MAJOR_VERSION" = "6" ]; then
            echo "   1. 群晖Docker服务异常"
        elif [ "$DSM_MAJOR_VERSION" = "7" ] && [ "$DSM_MINOR_VERSION" -le "1" ]; then
            echo "   1. 群晖Docker服务异常"
        else
            echo "   1. 群晖Container Manager服务异常"
        fi
    else
        echo "   1. systemd服务异常"
        echo "   2. Docker未安装或未运行"
    fi
    exit 1
fi

echo "[!] 注意事项："
if [ -f "/etc/synoinfo.conf" ]; then
    echo "群晖用户：data-root 已锁定官方默认路径"
    echo "DSM 版本：${DSM_VERSION}"
    if [ "$DSM_MAJOR_VERSION" = "6" ]; then
        echo "文件系统：${FS_TYPE}"
    fi
    echo "存储驱动：${STORAGE_DRIVER}"
else
    echo "通用系统：仅配置了镜像加速源"
fi